package com.cn.ZhiKeDianShang.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.cn.ZhiKeDianShang.R;
import com.cn.ZhiKeDianShang.model.response.ImageBean;
import com.cn.ZhiKeDianShang.util.view.SquareCenterImageView;

import java.util.ArrayList;

/**
 * 图片适配器
 */

public class GridViewAdapter extends BaseAdapter {

    ArrayList<ImageBean> mlocalPhotoList;
    int sumNumber;    //选择图片最多的数量
    String taskState; //任务状态为4 完成状态   加号图片不显示
    Context context;
    onGridViewAdapterClick monGridViewAdapterClick;

    @Override
    public int getCount() {
        if (mlocalPhotoList.size() >= sumNumber) {
            return sumNumber;
        }
        return mlocalPhotoList.size() + 1;
    }

    public GridViewAdapter() {
    }

    public GridViewAdapter(Context context, ArrayList<ImageBean> mlocalPhotoList, int sumNumber, String taskState, onGridViewAdapterClick onGridViewAdapterClick) {
        this.mlocalPhotoList = mlocalPhotoList;
        this.sumNumber = sumNumber;
        this.context = context;
        this.taskState = taskState;
        this.monGridViewAdapterClick = onGridViewAdapterClick;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (mlocalPhotoList.size() == 0) {
            // View Type 1代表添加更多的视图
            return 1;
        } else if (mlocalPhotoList.size() < sumNumber) {
            if (position < mlocalPhotoList.size()) {
                // View Type 0代表普通的ImageView视图
                return 0;
            } else {
                return 1;
            }
        } else {
            return 0;
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
//        LogUtil.d("Debug", "到达getView里面-------" + getItemViewType(position));
        View view = LayoutInflater.from(context)
                .inflate(R.layout.layout_circle_add_more_item_temp, parent, false);
        SquareCenterImageView iv = (SquareCenterImageView) view.findViewById(R.id.iv);
        ImageView tv_delete = (ImageView) view.findViewById(R.id.tv_delete);
        RelativeLayout rl_item = (RelativeLayout) view.findViewById(R.id.rl_item);

        rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monGridViewAdapterClick.onItemClick(position);
            }
        });
        tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monGridViewAdapterClick.delete(position);
            }
        });

        if (getItemViewType(position) == 0) { // 普通的视图
            if (!taskState.equals("4")) {
                tv_delete.setVisibility(View.VISIBLE);
            } else {
                tv_delete.setVisibility(View.GONE);
            }
            if (TextUtils.isEmpty(mlocalPhotoList.get(position).getLocalUrl())) {
                Glide.with(context)
                        .load(mlocalPhotoList.get(position).getOssUrl())
                        .into(iv);
            } else {
                Glide.with(context)
                        .load(mlocalPhotoList.get(position).getLocalUrl())
                        .into(iv);
            }

        } else {
            tv_delete.setVisibility(View.GONE);
            if (!taskState.equals("4")) {
                iv.setImageResource(R.mipmap.ddxx_zxj);
            }

        }
        return view;
    }


    public interface onGridViewAdapterClick {
        void onItemClick(int position);

        void delete(int position);
    }

}
